﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyLogProject
{
    public static class LogManager
    {

        private static Object obj = new object();

        #region 属性
        private static string DefaultLogPath
        {
            get
            {
                string strPath = AppDomain.CurrentDomain.BaseDirectory + "\\AppLog";
                if (!Directory.Exists(strPath))
                {
                    Directory.CreateDirectory(strPath);
                }
                return strPath;
            }
        }

        static LogManager()
        {
            //InstanceBase<NetWorkControl>.Instance.InitDate();
        }

        #endregion
        /// <summary>
        /// 一般日志类容记录
        /// </summary>
        /// <param name="logType">日志类型（异常类型）</param>
        /// <param name="logContent">日志内容</param>
        /// <param name="filePath">日志文件目录</param>
        public static void WriteLog(LogTypeEnum logType, string MethodName, string logContent, string filePath = "")
        {
            Task.Run(() => {
                try
                {
                    lock (obj)
                    {
                        string logPath = GetFileName(logType, filePath);
                        string strContent = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "(" + MethodName + ")" + ":" + logContent + "\r\n";
                        File.AppendAllText(logPath, strContent);
                    }

                }
                catch (Exception ex)
                {

                }
            });
        }

        private static string GetFileName(LogTypeEnum logType, string filePath = "")
        {
            string FileName = "";
            switch (logType)
            {
                case LogTypeEnum.Error:
                    if (string.IsNullOrEmpty(filePath))
                    {
                        FileName = DefaultLogPath + "\\ErrorLog_" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
                    }
                    break;
                case LogTypeEnum.Warning:
                    if (string.IsNullOrEmpty(filePath))
                    {
                        FileName = DefaultLogPath + "\\WarningLog_" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
                    }
                    break;
                case LogTypeEnum.Normal:
                    if (string.IsNullOrEmpty(filePath))
                    {
                        FileName = DefaultLogPath + "\\NormalLog_" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
                    }
                    break;
                case LogTypeEnum.Exception:
                    if (string.IsNullOrEmpty(filePath))
                    {
                        FileName = DefaultLogPath + "\\ExceptionLog_" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
                    }
                    break;
                case LogTypeEnum.SqlType:
                    if (string.IsNullOrEmpty(filePath))
                    {
                        FileName = DefaultLogPath + "\\SqlLog_" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
                    }
                    break;
            }
            return FileName;
        }

        /// <summary>
        /// 带标识的协议日志记录
        /// </summary>
        /// <param name="logType"></param>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static string WriteUpperComputerLog(LogTypeEnum logType, byte[] bytes)
        {
            string strContent = "";
            try
            {
                string path = AppDomain.CurrentDomain.BaseDirectory + "AgreementLog.log";
                string logPath = path;

                string str = "";
                bool flag = bytes != null;
                if (flag)
                {
                    for (int i = 0; i < bytes.Length; i++)
                    {
                        str += bytes[i].ToString("X2");
                        str += " ";
                    }
                }
                strContent = logType.ToString() + ";" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ":" + str + "\r\n";
                File.AppendAllText(logPath, strContent);
            }
            catch (Exception ex)
            {

            }
            return strContent;
        }

        /// <summary>
        /// 不带标识的协议日志记录
        /// </summary>
        /// <param name="_path"></param>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static string WriteUpperComputerLog(string _path, byte[] bytes)
        {
            string strContent = "";
            try
            {
                string str = "";
                bool flag = bytes != null;
                if (flag)
                {
                    for (int i = 0; i < bytes.Length; i++)
                    {
                        str += bytes[i].ToString("X2");
                        str += " ";
                    }
                }
                strContent = DateTime.Now.ToString("HH:mm:ss:fff") + " " + str + "\r\n";
                File.AppendAllText(_path, strContent);
            }
            catch (Exception ex)
            {

            }
            return strContent;
        }

        /// <summary>
        /// 不带标识的协议日志记录 记录时间为主动传入
        /// </summary>
        /// <param name="_path"></param>
        /// <param name="itemTime"></param>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static string WriteUpperComputerLog(string _path, DateTime itemTime, byte[] bytes)
        {
            string strContent = "";
            try
            {
                string str = "";
                bool flag = bytes != null;
                if (flag)
                {
                    for (int i = 0; i < bytes.Length; i++)
                    {
                        str += bytes[i].ToString("X2");
                        str += " ";
                    }
                }
                strContent = itemTime.ToString("HH:mm:ss:fff") + " " + str + "\r\n";
                File.AppendAllText(_path, strContent);
            }
            catch (Exception ex)
            {

            }
            return strContent;
        }

    }
}
